package community.hospital.service.application.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.common.BaseRemove;
import community.hospital.common.CustomerException;
import community.hospital.convert.PageConvert;
import community.hospital.convert.ScheduleConvert;
import community.hospital.entity.Schedule;
import community.hospital.entity.request.ScheduleRequest;
import community.hospital.entity.request.ListScheduleRequest;
import community.hospital.entity.response.DoctorResponse;
import community.hospital.entity.response.ScheduleResponse;
import community.hospital.service.application.DoctorApplicationService;
import community.hospital.service.application.OrderApplicationService;
import community.hospital.service.application.ScheduleApplicationService;
import community.hospital.service.domain.ScheduleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static community.hospital.enums.BusinessEnum.DOCTOR_SCHEDULE_REPEAT;

/**
 * @author yindingjie
 */
@Service
public class ScheduleApplicationServiceImpl implements ScheduleApplicationService {
    @Resource
    private ScheduleService scheduleService;
    @Resource
    private DoctorApplicationService doctorApplicationService;
    @Resource
    private OrderApplicationService orderApplicationService;
    @Override
    public ScheduleResponse querySchedule(Long id) {
        Schedule schedule = scheduleService.getById(id);
        ScheduleResponse scheduleResponse = ScheduleConvert.INSTANCE.converToResp(schedule);
        DoctorResponse doctorResponse = doctorApplicationService.queryDoctor(schedule.getDoctorId());
        scheduleResponse.setDoctorResponse(doctorResponse);
        return scheduleResponse;
    }

    @Override
    public Page<ScheduleResponse> listSchedule(ListScheduleRequest request) {
        Page<Schedule> schedulePage = scheduleService.listSchedule(request);
        Page<ScheduleResponse> responsePage = PageConvert.convert(schedulePage, ScheduleConvert.INSTANCE::converToResp);
        for (int i = 0; i < responsePage.getRecords().size(); i++) {
            Schedule schedule = schedulePage.getRecords().get(i);
            DoctorResponse doctorResponse = doctorApplicationService.queryDoctor(schedule.getDoctorId());
            //查询剩余号源数量
            int count = orderApplicationService.countByScheduleId(schedule.getId());
            doctorResponse.setRemainingQuantity(doctorResponse.getRegisterQuantity()-count);
            responsePage.getRecords().get(i).setDoctorResponse(doctorResponse);
        }
        return responsePage;
    }

    @Override
    public Boolean addOrUpdateSchedule(ScheduleRequest request) {
        Date date = request.getDetailTime();
        LocalDateTime dateTime = LocalDateTimeUtil.of(date);
        int year = dateTime.getYear();
        int month = dateTime.getMonth().getValue();
        int day = dateTime.getDayOfMonth();
        //判断医生当天是否排过班
        int count = scheduleService.countByDoctorIdAndTime(year, month, day, request.getDoctorId());
        if (count > 0) {
            throw new CustomerException(DOCTOR_SCHEDULE_REPEAT);
        }
        Schedule schedule = ScheduleConvert.INSTANCE.convertToSchedule(request);

        schedule.setYear(year);
        schedule.setMonth(month);
        schedule.setDay(day);
        return scheduleService.saveOrUpdate(schedule);
    }

    @Override
    public Boolean removeSchedule(List<Long> ids) {
        return scheduleService.removeBatchByIds(ids);
    }

    @Override
    public ScheduleResponse querySchedule(Long doctorId, LocalDate localDate) {
        Schedule schedule = scheduleService.querySchedule(doctorId, localDate);
        ScheduleResponse scheduleResponse = ScheduleConvert.INSTANCE.converToResp(schedule);
//        DoctorResponse doctorResponse = doctorApplicationService.queryDoctor(doctorId);
//        scheduleResponse.setDoctorResponse(doctorResponse);
        return scheduleResponse;
    }


}
